package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书...");
        System.out.println("请输入你要删除的书籍名称：");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int pos = -1;
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(bookName)) {
                //如果找到这本书就记录它的下标
                pos = i;
                break;
            }
        }
        if (i >= currentSize) {
            System.out.println("没有找到这本书！");
            return;
        }
        //开始删除
        for (int j = 0; j < currentSize - 1; j++) {
            Book book = bookList.getBook(j + 1);
            bookList.setBook(j, book);
        }
        System.out.println("删除图书成功！");
        //修改setUsedSize
        //把后一个位置的图书往前一个位置存放
        bookList.setUsedSize(currentSize - 1);
        //将最后一个位置置为空
        bookList.setBook(currentSize - 1, null);
    }
}
